<?php

/*************************************************************************
 * Zero PHP
 * Copyright (c) 2012
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * Author: zlk
 *************************************************************************/
class CZero {
	//定义对象实例的容器,缓存实例
	private static $_instance = array ();

	/**
	 +----------------------------------------------------------
	 * 自动变量设置
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param $name 属性名称
	 * @param $value  属性值
	 +----------------------------------------------------------
	 */
	//检查类里面是否有该属性，如果有重置
	public function __set($name, $value) {
		if (property_exists($this, $name))
			$this-> $name = $value;
		$setter = 'set' . $name;
		if (method_exists($this, $setter))
			$this-> $setter ($value);
	}

	/**
	 +----------------------------------------------------------
	 * 自动变量获取
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param $name 属性名称
	 +----------------------------------------------------------
	 * @return mixed 
	 +----------------------------------------------------------
	 */
	//检查变量是否存在，如果有则返回
	public function __get($name) {
		if (isset ($this-> $name))
			return $this-> $name;
		$getter = 'get' . $name;
		if (method_exists($this, $getter))
			return $this-> $getter;
	}

	public static function instance($class, $method) {
		$key = $class . $method;
		if (!isset (self :: $_instance[$key])) {
			$obj = new $class ();
			if (!empty ($method) && method_exists($obj, $method)) {
				self :: $_instance[$key] = call_user_func_array(array (
					$obj,
					$method
				), array ());
			} else {
				self :: $_instance = $obj;
			}
		}
		return self :: $_instance[$class . $method];
	}

//实例化action
	public static function newAction($name) {
		static $_action = array();
		if(isset($_action['Action'.$name])) return $_action['Action'.$name];
		//判断是否分组
		 if (strpos($name, '.')) {
		 	$explode=explode('.',$name);
		 	$classname = array_pop($explode).'Action';
		 	import('Action.'.$name);
		 }else{
		 	$classname=$name.'Action';
		 	import('Action.'.$classname);
		 }
		 //缓存实例
		 if(class_exists($classname)){
		 	$_action['Action'.$name]=new $classname();
		 	return $_action['Action'.$name];
		 }else{
		 	return false;
		 }
	}

	//清空缓存和config
	public static function refresh() {
	}

	public static function version() {
		return ZERO_VERSION;
	}

} //类定义结束
?>